package com.yisin.pbox.www.common.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import com.yisin.pbox.frame.common.RootContext;
import com.yisin.pbox.frame.util.FileUtils;
import com.yisin.pbox.frame.util.StringUtil;
import com.yisin.pbox.www.common.frame.BaseAction;

/**
 * @简述：kindeditor 编辑器文件上传处理类
 * @author admin
 * @since 1.0
 */
@SuppressWarnings("serial")
public class UploadFileActionForKindeditor extends BaseAction {

	private File imgFile;
	private File uploadFile;
	private String localUrl;
	private String dir;
	private float fileMaxSize = 204800;
	private String dirName = "/attached/";

	private String fileSavePath = "/";

	/**
	 * 设置文件保存路径
	 * 
	 * @param request
	 */
	public void setFileSavePath(HttpServletRequest request) {
		String spath = request.getRealPath("");
		int index = spath.lastIndexOf("\\");
		fileSavePath = spath.substring(0, index + 1)
				+ RootContext.getRootName().substring(1) + dirName;
		FileUtils.createDir(fileSavePath);
	}

	/**
	 * 获取文件保存路径
	 * 
	 * @param request
	 */
	public String getFileSavePath() {
		return fileSavePath;
	}

	public String getFileName() {
		return FileUtils.getNameToPath(localUrl);
	}

	public void upload() {
		System.out.println("In...UploadFileActionForKindeditor#upload");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			float fileSize = StringUtil.stringToFloat(
					FileUtils.siSheWuRu((uploadFile.length() / (float) 1024)),
					0f);
			if (fileSize > fileMaxSize) {
				pw.write(outMessage("文件大小超过限制！", 1));
				return;
			}
			String sname = getFileName();
			String fileName = StringUtil.getUniqueId("f");
			String zui = FileUtils.getFileZui(sname);
			String dir = FileUtils.getFileGenre(sname);
			if (zui.equals("")) {
				zui = ".swf";
				dir = "flash";
			}
			dirName += dir;
			setFileSavePath(request);
			// 以服务器的文件保存地址和原文件名建立上传文件输出流
			FileOutputStream fos = new FileOutputStream(getFileSavePath() + "/"
					+ fileName + zui);
			// 以上传文件建立一个文件上传流
			FileInputStream fis = new FileInputStream(uploadFile);
			// 将上传文件的内容写入服务器
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			fis.close();
			fos.flush();
			fos.close();

			JSONObject obj = new JSONObject();
			obj.put("error", 0);
			obj.put("url", RootContext.getRootName() + dirName + "/" + fileName
					+ zui);
			pw.write(obj.toString());
		} catch (Exception e) {
			pw.write(outMessage("上传失败！", 1));
			e.printStackTrace();
		}
	}

	public String outMessage(String message, int genre) {
		JSONObject obj = new JSONObject();
		obj.put("error", genre);
		obj.put("message", message);
		return obj.toString();
	}

	public void setFileMaxSize(float fileMaxSize) {
		this.fileMaxSize = fileMaxSize;
	}

	public float getFileMaxSize() {
		return fileMaxSize;
	}

	public File getImgFile() {
		return imgFile;
	}

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	public String getLocalUrl() {
		return localUrl;
	}

	public void setLocalUrl(String localUrl) {
		this.localUrl = localUrl;
	}

	/**
	 * @param uploadFile
	 *            the uploadFile to set
	 */
	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	/**
	 * @return the uploadFile
	 */
	public File getUploadFile() {
		return uploadFile;
	}

	/**
	 * @param dir
	 *            the dir to set
	 */
	public void setDir(String dir) {
		this.dir = dir;
	}

	/**
	 * @return the dir
	 */
	public String getDir() {
		return dir;
	}
}
